﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.Filter;

namespace Cofe.Web.Models
{
    public class ExplorerViewModel
    {
        #region Constructor

        protected ExplorerViewModel(ICofeDirectoryInfo rootDirectory, bool listContainer, bool listItem)
        {
            _rootDirectoryModel = EntryViewModel<ICofeDirectoryInfo>.FromEntry(rootDirectory);

            if (listItem || listContainer)
            {

                IFilter filter =
                    new OrEntryFilter(
                        new FileTypeEntryFilter(FileTypeEntryFilter.Folder),
                        new FileTypeEntryFilter(FileTypeEntryFilter.File)
                        );
                if (listContainer != listItem)
                    if (listItem)
                        filter = new FileTypeEntryFilter(FileTypeEntryFilter.File);
                    else filter = new FileTypeEntryFilter(FileTypeEntryFilter.Folder);

                _subEntryModels = (from e in rootDirectory.EnumerateCofeSystemInfos(
                                       FilterCollections.FromFilters(filter),
                                       EntryConstructionFlags.Default, CofeServices.EntryConstructor)
                                   select EntryViewModel.FromEntry(e)).ToList();
            }            
        }

        public static ExplorerViewModel FromDirectory(ICofeDirectoryInfo rootDirectory, 
            bool listContainer = true, bool listItem = true)
        {
            return new ExplorerViewModel(rootDirectory, listContainer, listItem);
        }
        
        #endregion

        #region Methods
        
        
        #endregion

        #region Data

        EntryViewModel<ICofeDirectoryInfo> _rootDirectoryModel;
        List<EntryViewModel> _subEntryModels;
        
        #endregion

        #region Public Properties

        public EntryViewModel<ICofeDirectoryInfo> RootDirectory { get { return _rootDirectoryModel; } }
        public IEnumerable<EntryViewModel> SubEntries { get { return _subEntryModels; } }        

        #endregion

    }
}
